package com.mao.service.impl.product;

import java.io.OutputStream;
import java.util.List;

import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mao.dao.product.ProductClassDao;
import com.mao.dao.product.ProductDao;
import com.mao.model.product.Product;
import com.mao.model.product.ProductClass;
import com.mao.model.warehouse.WareHouse;
import com.mao.service.product.IProductService;
import com.mao.util.Validator;

@Service("productService")
public class ProductServiceImpl implements IProductService {

	private ProductDao productDao;

	private ProductClassDao productClassDao;

	public Product saveProduct(Product pro) {
		return productDao.saveProduct(pro);
	}

	public Product updateProduct(Product pro) {
		return productDao.updateProduct(pro);
	}

	public List<Product> getAllProducts(ProductCondition productCondition,
			Integer page, Integer rows) {
		return productDao.getAllProducts(productCondition, page, rows);
	}

	public List<Product> getAllProducts(ProductCondition productCondition) {
		return productDao.getAllProducts(productCondition);
	}

	public int getAllProductCount(ProductCondition productCondition) {
		return productDao.getAllProductCount(productCondition);
	}

	public Product getProductById(long id) {
		return productDao.getProductById(id);
	}

	public List<Product> getProductsByProClass(
			ProductCondition productCondition, Integer page, Integer rows) {
		return productDao.getProductsByProClass(productCondition, page, rows);
	}
	
	public Integer getProductsByProClassCount(ProductCondition productCondition) {
		return productDao.getAllProductCount(productCondition);
	}

	public String valDelPro(long id) {
		return productDao.valDelPro(id);
	}

	private WritableCellFormat getCellStyle(WritableCellFormat wcf, int flag)
			throws WriteException {
		wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
		// 设置单元格内容对齐方式
		if (flag == 0)
			wcf.setAlignment(jxl.format.Alignment.LEFT);
		else if (flag == 1)
			wcf.setAlignment(jxl.format.Alignment.RIGHT);
		else
			wcf.setAlignment(jxl.format.Alignment.CENTRE);
		// 设置单元格内容为垂直居中
		wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
		return wcf;
	}

	public void exportExcel(ProductCondition productCondition, OutputStream os) {
		List<Product> list = this.getAllProducts(productCondition);
		if (list != null && list.size() > 0) {
			try {
				String title = "商品清单";
				if (!Validator.isEmpty(productCondition.getPcid())
						&& productCondition.getPcid() != 0) {
					ProductClass pc = this.productClassDao
							.getProductClassById(productCondition.getPcid());
					title = pc.getName() + "的" + title;
				}

				// 创建文件
				WritableWorkbook book = Workbook.createWorkbook(os);
				// 生成名为“第一页”的工作表，参数0表示这是第一页
				WritableSheet sheet = book.createSheet("第一页 ", 0);
				sheet.mergeCells(0, 0, 5, 0);
				WritableFont font = new WritableFont(WritableFont.TIMES, 12,
						WritableFont.BOLD);
				WritableCellFormat leftFormat = new WritableCellFormat(font);// 标题靠左
				leftFormat = getCellStyle(leftFormat, 0);
				WritableCellFormat rightFormat = new WritableCellFormat(font);// 标题靠右
				rightFormat = getCellStyle(rightFormat, 1);

				WritableCellFormat format = new WritableCellFormat();// 内容靠左
				format = getCellStyle(format, 0);

				NumberFormat numFmt = new NumberFormat("#.00");
				WritableCellFormat format2 = new WritableCellFormat(numFmt);// 内容靠右
				format2 = getCellStyle(format2, 1);

				WritableCellFormat centerFormat = new WritableCellFormat(font);// 标题居中
				centerFormat = getCellStyle(centerFormat, 3);
				WritableCellFormat format3 = new WritableCellFormat();// 日期居中
				format3 = getCellStyle(format3, 3);

				sheet.setColumnView(0, 30);
				sheet.setColumnView(1, 25);
				sheet.setColumnView(2, 15);
				sheet.setColumnView(3, 15);
				sheet.setColumnView(4, 20);
				sheet.setColumnView(5, 20);

				sheet.addCell(new Label(0, 0, title, centerFormat));
				sheet.addCell(new Label(0, 1, "编号", leftFormat));
				sheet.addCell(new Label(1, 1, "名称", leftFormat));
				sheet.addCell(new Label(2, 1, "商品分类", leftFormat));
				sheet.addCell(new Label(3, 1, "规格", leftFormat));
				sheet.addCell(new Label(4, 1, "零售价（元）", rightFormat));
				sheet.addCell(new Label(5, 1, "成本价（元）", rightFormat));

				for (int i = 0; i < list.size(); i++) {
					Product pro = list.get(i);
					int j = 0;
					sheet.addCell(new Label(j, i + 2, pro.getProNo(), format));
					sheet.addCell(new Label(j + 1, i + 2, pro.getName(), format));
					sheet.addCell(new Label(j + 2, i + 2, pro.getProClass().getName(), format));
					sheet.addCell(new Label(j + 3, i + 2, pro.getSpec(), format));
					sheet.addCell(new jxl.write.Number(j + 4, i + 2, pro
							.getRetailPrice().doubleValue(), format2));
					sheet.addCell(new jxl.write.Number(j + 5, i + 2, Double
							.valueOf(pro.getLowestPrice().toString())
							.doubleValue(), format2));

				}
				book.write();
				book.close();
			} catch (Exception e) {
				System.out.println(e);
			}
		}
	}
	
	public List<Product> getAllProducts() {
		return productDao.getAllProducts();
	}

	public Product getProductByInitAndProId(Long proId,WareHouse warehouse) {
		return productDao.getProductByInitAndProId(proId, warehouse);
	}
	
	public ProductDao getProductDao() {
		return productDao;
	}

	@Autowired
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public ProductClassDao getProductClassDao() {
		return productClassDao;
	}

	@Autowired
	public void setProductClassDao(ProductClassDao productClassDao) {
		this.productClassDao = productClassDao;
	}

}
